package com.ccubee.data.transfer.transfer.mod.impl;

import cn.hutool.core.collection.CollUtil;
import com.ccubee.data.transfer.event.ColumnData;
import com.ccubee.data.transfer.event.IEventType;
import com.ccubee.data.transfer.event.SqlData;
import com.ccubee.data.transfer.transfer.mod.AbstractMod;
import com.ccubee.data.transfer.transfer.mod.Imod;
import com.ccubee.data.transfer.types.TransferMod;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 分页模式
 *
 * @Author ycf
 * @Date 2022/11/14 14:37
 * @Version 1.0
 */
@Component
public class PageMod extends AbstractMod implements Imod {

    @Override
    public TransferMod mod() {
        return TransferMod.PAGE;
    }

    @Override
    protected SqlData sqlData(String dbName, String tableName, long num) {
        return SqlData
                .builder()
                .sql(String.format(IEventType.SELECT_SQL, dbName, tableName, mod().sql(transferRequest)))
                .data(Lists.newArrayList(num, transferRequest.getPageSize()))
                .build();
    }

    @Override
    protected long num(List<List<ColumnData>> maps, long num) {
        return CollUtil.isEmpty(maps) ? -1 : num + 1;
    }
}
